package com.xwli.springcloud.gof23.strategy;

import com.xwli.springcloud.gof23.strategy.behavior.FlyBehavior;
import com.xwli.springcloud.gof23.strategy.behavior.QuackBehavior;

/**
 * @author xwli
 * @date: 2022/2/25 10:13
 * @desc  策略模式
 * @since JDK 1.8
 */
public abstract class Duck {

    protected QuackBehavior quackBehavior; //变化的叫法被抽象成接口

    protected FlyBehavior flyBehavior; //变化的飞行被抽象成接口

    public void performQuack(){
        quackBehavior.quack();
    }

    public void performFly(){
        flyBehavior.fly();
    }

    public void swim(){ //公共不变的行为保留
        System.out.println("所有的鸭子都会游泳");
    }

    public abstract void display(); //鸭子的外观都不一样

    public void setFlyBehavior(FlyBehavior fb){ //动态的更改飞行行为
        flyBehavior = fb;
    }

    public void setQuackBehavior(QuackBehavior qb){ //动态的更改叫声行为
        quackBehavior = qb;
    }
}

